home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / docs.lha / cmu-user / cmu-user.info-7 < prev    next >
Text File  |  1992-08-05  |  51KB  |  1,381 lines

  1. Info file: cmu-user.info,    -*-Text-*-
  2. produced by latexinfo-format-buffer
  3. from file: cmu-user.tex
  4.  
  5.  
  6. 
  7. File: cmu-user.info  Node: Representation Efficiency Notes, Prev: Efficiency Notes and Type Checking, Up: Efficiency Notes, Next: Verbosity Control
  8.  
  9. Representation Efficiency Notes
  10. -------------------------------
  11.  
  12.  
  13. When operating on values that have non-descriptor representations (*Note
  14. Non-Descriptor Representations::), there can be a substantial time and
  15. consing penalty for converting to and from descriptor representations.
  16. For this reason, the compiler gives an efficiency note whenever it is
  17. forced to do a representation coercion more expensive than
  18. efficiency-note-cost-threshold ?.
  19.  
  20. Inefficient representation coercions may be due to type uncertainty, as in this example:
  21.      
  22.      (defun set-flo (x)
  23.        (declare (single-float x))
  24.        (prog ((var 0.0))
  25.          (setq var (gorp))
  26.          (setq var x)
  27.          (return var)))
  28.  
  29. which produces this efficiency note:
  30.      
  31.      In: DEFUN SET-FLO
  32.        (SETQ VAR X) Note: Doing float to pointer coercion (cost 13) from
  33.      X to VAR.
  34.  
  35. The variable `var' is not known to always hold values of type
  36. `single-float', so a descriptor representation must be used for its
  37. value.  In sort of situation, and adding a declaration will eliminate
  38. the inefficiency.
  39.  
  40. Often inefficient representation conversions are not due to type uncertainty
  41. -- instead, they result from evaluating a non-descriptor expression in a
  42. context that requires a descriptor result:
  43.      
  44.    * Assignment to or initialization of any data structure other than a specialized
  45.      array (*Note Specialized Arrays::), or
  46.      
  47.    * Assignment to a `special' variable, or
  48.      
  49.    * Passing as an argument or returning as a value in any function call that is not
  50.      a local call (*Note Interactions With Local Call::.)
  51.  
  52.  
  53. If such inefficient coercions appear in a "hot spot" in the program,
  54. data structures redesign or program reorganization may be necessary to
  55. improve efficiency.  See sections *Note Block Compilation::, *Note
  56. Numbers:: and ?.
  57.  
  58. Because representation selection is done rather late in compilation, the source
  59. context in these efficiency notes is somewhat vague, making interpretation more
  60. difficult.  This is a fairly straightforward example:
  61.      
  62.      (defun cf+ (x y)
  63.        (declare (single-float x y))
  64.        (cons (+ x y) t))
  65.  
  66. which gives this efficiency note:
  67.      
  68.      In: DEFUN CF+
  69.        (CONS (+ X Y) T)
  70.      Note: Doing float to pointer coercion (cost 13), for:
  71.            The first argument of CONS.
  72.  
  73. The source context form is almost always the form that receives the value being
  74. coerced (as it is in the preceding example), but can also be the source form
  75. which generates the coerced value.  Compiling this example:
  76.      
  77.      (defun if-cf+ (x y)
  78.        (declare (single-float x y))
  79.        (cons (if (grue) (+ x y) (snoc)) t))
  80.  
  81. produces this note:
  82.      
  83.      In: DEFUN IF-CF+
  84.        (+ X Y) Note: Doing float to pointer coercion (cost 13).
  85.  
  86.  
  87. In either case, the note's text explanation attempts to include additional
  88. information about what locations are the source and destination of the
  89. coercion.  Here are some example notes:
  90.      
  91.        (IF (GRUE) X (SNOC)) Note: Doing float to pointer coercion (cost
  92.      13) from X.
  93.      
  94.        (SETQ VAR X) Note: Doing float to pointer coercion (cost 13) from
  95.      X to VAR.
  96.  
  97. Note that the return value of a function is also a place to which coercions may
  98. have to be done:
  99.      
  100.        (DEFUN F+ (X Y) (DECLARE (SINGLE-FLOAT X Y)) (+ X Y)) Note: Doing
  101.      float to pointer coercion (cost 13) to "<return value>".
  102.  
  103. Sometimes the compiler is unable to determine a name for the source or
  104. destination, in which case the source context is the only clue.
  105.  
  106.  
  107. 
  108. File: cmu-user.info  Node: Verbosity Control, Prev: Representation Efficiency Notes, Up: Efficiency Notes
  109.  
  110. Verbosity Control
  111. -----------------
  112.  
  113.  
  114. These variables control the verbosity of efficiency notes:
  115.  
  116.  
  117.  
  118.  -- Variable: *efficiency-note-cost-threshold*
  119.      Before printing some efficiency notes, the compiler compares the
  120.      value of this variable to the difference in cost between the chosen
  121.      implementation and the best potential implementation.  If the
  122.      difference is not greater than this limit, then no note is printed.
  123.      The units are implementation dependent; the initial value
  124.      suppresses notes about "trivial" inefficiencies.  A value of `1'
  125.      will note any inefficiency.
  126.  
  127.  
  128.  
  129.  
  130.  -- Variable: *efficiency-note-limit*
  131.      When printing some efficiency notes, the compiler reports possible
  132.      efficient implementations.  The initial value of `2' prevents
  133.      excessively long efficiency notes in the common case where there is
  134.      no type information, so all implementations are possible.
  135.  
  136.  
  137.  
  138. 
  139. File: cmu-user.info  Node: Profiling, Prev: Efficiency Notes, Up: Advanced Compiler Use and Efficiency Hints
  140.  
  141. Profiling
  142. =========
  143.  
  144.  
  145.  
  146. The first step in improving a program's performance is to profile the activity
  147. of the program to find where it spends its time.  The best way to do this is to
  148. use the profiling utility found in the `profile' package.  This package
  149. provides a macro `profile' that encapsulates functions with statistics
  150. gathering code.  
  151.  
  152. * Menu:
  153.  
  154. * Profile Interface::           
  155. * Profiling Techniques::        
  156. * Nested or Recursive Calls::   
  157. * Clock resolution::            
  158. * Profiling overhead::          
  159. * Additional Timing Utilities::  
  160. * A Note on Timing::            
  161. * Benchmarking Techniques::     
  162.  
  163.  
  164. 
  165. File: cmu-user.info  Node: Profile Interface, Prev: Profiling, Up: Profiling, Next: Profiling Techniques
  166.  
  167. Profile Interface
  168. -----------------
  169.  
  170.  
  171.  
  172.  
  173.  -- Variable: *timed-functions*
  174.      This variable holds a list of all functions that are currently
  175.      being profiled.
  176.  
  177.  
  178.  
  179.  -- Macro: profile  {NAME}*
  180.  
  181.      This macro wraps profiling code around the named functions.  As in
  182.      `trace', the NAMEs are not evaluated.  If a function is already
  183.      profiled, then the function is unprofiled and reprofiled (useful to
  184.      notice function redefinition.)  A warning is printed for each name
  185.      that is not a defined function.
  186.  
  187.  
  188.  
  189.  -- Macro: unprofile  {NAME}*
  190.  
  191.      This macro removes profiling code from the named functions.  If no
  192.      NAMEs are supplied, all currently profiled functions are
  193.      unprofiled.
  194.  
  195.  
  196.  
  197.  
  198.  -- Macro: report-time  {NAME}*
  199.  
  200.      This macro prints a report for each NAMEd function of the following
  201.      information:
  202.         * The total CPU time used in that function for all calls,
  203.           
  204.         * the total number of bytes consed in that function for all calls,
  205.           
  206.         * the total number of calls,
  207.           
  208.         * the average amount of CPU time per call.
  209.      
  210.      Summary totals of the CPU time, consing and calls columns are
  211.      printed.  An estimate of the profiling overhead is also printed
  212.      (see below).  If no NAMEs are supplied, then the times for all
  213.      currently profiled functions are printed.
  214.  
  215.  
  216.  
  217.  -- Macro: reset-time  {NAME}*
  218.  
  219.      This macro resets the profiling counters associated with the NAMEd
  220.      functions.  If no NAMEs are supplied, then all currently profiled
  221.      functions are reset.
  222.  
  223.  
  224.  
  225. 
  226. File: cmu-user.info  Node: Profiling Techniques, Prev: Profile Interface, Up: Profiling, Next: Nested or Recursive Calls
  227.  
  228. Profiling Techniques
  229. --------------------
  230.  
  231.  
  232. Start by profiling big pieces of a program, then carefully choose which
  233. functions close to, but not in, the inner loop are to be profiled next.
  234. Avoid profiling functions that are called by other profiled functions,
  235. since this opens the possibility of profiling overhead being included in
  236. the reported times.
  237.  
  238. If the per-call time reported is less than 1/10 second, then consider
  239. the clock resolution and profiling overhead before you believe the time.
  240. It may be that you will need to run your program many times in order to
  241. average out to a higher resolution.
  242.  
  243.  
  244. 
  245. File: cmu-user.info  Node: Nested or Recursive Calls, Prev: Profiling Techniques, Up: Profiling, Next: Clock resolution
  246.  
  247. Nested or Recursive Calls
  248. -------------------------
  249.  
  250.  
  251. The profiler attempts to compensate for nested or recursive calls.  Time
  252. and consing overhead will be charged to the dynamically innermost (most
  253. recent) call to a profiled function.  So profiling a subfunction of a
  254. profiled function will cause the reported time for the outer function to
  255. decrease.  However if an inner function has a large number of calls,
  256. some of the profiling overhead may "leak" into the reported time for the
  257. outer function.  In general, be wary of profiling short functions that
  258. are called many times.
  259.  
  260. 
  261. File: cmu-user.info  Node: Clock resolution, Prev: Nested or Recursive Calls, Up: Profiling, Next: Profiling overhead
  262.  
  263. Clock resolution
  264. ----------------
  265.  
  266.  
  267. Unless you are very lucky, the length of your machine's clock "tick" is
  268. probably much longer than the time it takes simple function to run.  For
  269. example, on the IBM RT, the clock resolution is 1/50 second.  This means that
  270. if a function is only called a few times, then only the first couple decimal
  271. places are really meaningful.  
  272.  
  273. Note however, that if a function is called many times, then the
  274. statistical averaging across all calls should result in increased
  275. resolution.  For example, on the IBM RT, if a function is called a
  276. thousand times, then a resolution of tens of microseconds can be
  277. expected.
  278.  
  279. 
  280. File: cmu-user.info  Node: Profiling overhead, Prev: Clock resolution, Up: Profiling, Next: Additional Timing Utilities
  281.  
  282. Profiling overhead
  283. ------------------
  284.  
  285.  
  286. The added profiling code takes time to run every time that the profiled
  287. function is called, which can disrupt the attempt to collect timing
  288. information.  In order to avoid serious inflation of the times for
  289. functions that take little time to run, an estimate of the overhead due
  290. to profiling is subtracted from the times reported for each function.
  291.  
  292. Although this correction works fairly well, it is not totally accurate,
  293. resulting in times that become increasingly meaningless for functions
  294. with short runtimes.  This is only a concern when the estimated
  295. profiling overhead is many times larger than reported total CPU time.
  296.  
  297. The estimated profiling overhead is not represented in the reported
  298. total CPU time.  The sum of total CPU time and the estimated profiling
  299. overhead should be close to the total CPU time for the entire profiling
  300. run (as determined by the `time' macro.)  Time unaccounted for is
  301. probably being used by functions that you forgot to profile.
  302.  
  303. 
  304. File: cmu-user.info  Node: Additional Timing Utilities, Prev: Profiling overhead, Up: Profiling, Next: A Note on Timing
  305.  
  306. Additional Timing Utilities
  307. ---------------------------
  308.  
  309.  
  310.  
  311.  -- Macro: time  FORM
  312.  
  313.      This macro evaluates FORM, prints some timing and memory allocation
  314.      information to `*trace-output*', and returns any values that FORM
  315.      returns.  The timing information includes real time, user run time, and system
  316.      run time.    This macro executes a form and reports the time and consing
  317.      overhead.  If the `time' form is not compiled (e.g. it was typed at
  318.      top-level), then `compile' will be called on the form to give more accurate
  319.      timing information.  If you really want to time interpreted speed, you can say:
  320.           
  321.           (time (eval 'FORM))
  322.      
  323.      Things that execute fairly quickly should be timed more than once, since there
  324.      may be more paging overhead in the first timing.  To increase the accuracy of
  325.      very short times, you can time multiple evaluations:
  326.           
  327.           (time (dotimes (i 100) FORM))
  328.      
  329.  
  330.  
  331.  
  332.  -- Function: get-bytes-consed
  333.      This function returns the number of bytes allocated since the first
  334.      time you called it.  The first time it is called it returns zero.
  335.      The above profiling routines use this to report consing
  336.      information.
  337.  
  338.  
  339.  
  340.  
  341.  -- Variable: *gc-run-time*
  342.      This variable accumulates the run-time consumed by garbage
  343.      collection, in the units returned by get-internal-run-time.
  344.  
  345.  
  346.  
  347.  -- Constant: internal-time-units-per-second
  348.      The value of internal-time-units-per-second is 100.
  349.  
  350.  
  351. 
  352. File: cmu-user.info  Node: A Note on Timing, Prev: Additional Timing Utilities, Up: Profiling, Next: Benchmarking Techniques
  353.  
  354. A Note on Timing
  355. ----------------
  356.  
  357.  
  358. There are two general kinds of timing information provided by the `time'
  359. macro and other profiling utilities: real time and run time.  Real time
  360. is elapsed, wall clock time.  It will be affected in a fairly obvious
  361. way by any other activity on the machine.  The more other processes
  362. contending for CPU and memory, the more real time will increase.  This
  363. means that real time measurements are difficult to replicate, though
  364. this is less true on a dedicated workstation.  The advantage of real
  365. time is that it is real.  It tells you really how long the program took
  366. to run under the benchmarking conditions.  The problem is that you don't
  367. know exactly what those conditions were.
  368.  
  369. Run time is the amount of time that the processor supposedly spent
  370. running the program, as opposed to waiting for I/O or running other
  371. processes.  "User run time" and "system run time" are numbers reported
  372. by the Unix kernel.  They are supposed to be a measure of how much time
  373. the processor spent running your "user" program (which will include GC
  374. overhead, etc.), and the amount of time that the kernel spent running
  375. "on your behalf".
  376.  
  377. Ideally, user time should be totally unaffected by benchmarking
  378. conditions; in reality user time does depend on other system activity,
  379. though in rather non-obvious ways.
  380.  
  381. System time will clearly depend on benchmarking conditions.  In Lisp
  382. benchmarking, paging activity increases system run time (but not by as much
  383. as it increases real time, since the kernel spends some time waiting for
  384. the disk, and this is not run time, kernel or otherwise.)
  385.  
  386. In my experience, the biggest trap in interpreting kernel/user run time
  387. is to look only at user time.  In reality, it seems that the SUM of
  388. kernel and user time is more reproducible.  The problem is that as
  389. system activity increases, there is a spurious DECREASE in user run
  390. time.  In effect, as paging, etc., increases, user time leaks into
  391. system time.
  392.  
  393. So, in practice, the only way to get truly reproducible results is to
  394. run with the same competing activity on the system.  Try to run on a
  395. machine with nobody else logged in, and check with "ps aux" to see if
  396. there are any system processes munching large amounts of CPU or memory.
  397. If the ratio between real time and the sum of user and system time
  398. varies much between runs, then you have a problem.
  399.  
  400. 
  401. File: cmu-user.info  Node: Benchmarking Techniques, Prev: A Note on Timing, Up: Profiling
  402.  
  403. Benchmarking Techniques
  404. -----------------------
  405.  
  406.  
  407. Given these imperfect timing tools, how do should you do benchmarking?
  408. The answer depends on whether you are trying to measure improvements in
  409. the performance of a single program on the same hardware, or if you are
  410. trying to compare the performance of different programs and/or different
  411. hardware.
  412.  
  413. For the first use (measuring the effect of program modifications with
  414. constant hardware), you should look at BOTH system+user and real time to
  415. understand what effect the change had on CPU use, and on I/O (including
  416. paging.)  If you are working on a CPU intensive program, the change in
  417. system+user time will give you a moderately reproducible measure of
  418. performance across a fairly wide range of system conditions.  For a CPU
  419. intensive program, you can think of system+user as "how long it would
  420. have taken to run if I had my own machine."  So in the case of comparing
  421. CPU intensive programs, system+user time is relatively real, and
  422. reasonable to use.
  423.  
  424. For programs that spend a substantial amount of their time paging, you
  425. really can't predict elapsed time under a given operating condition
  426. without benchmarking in that condition.  User or system+user time may be
  427. fairly reproducible, but it is also relatively meaningless, since in a
  428. paging or I/O intensive program, the program is spending its time
  429. waiting, not running, and system time and user time are both measures of
  430. run time.  A change that reduces run time might increase real time by
  431. increasing paging.
  432.  
  433. Another common use for benchmarking is comparing the performance of the
  434. same program on different hardware.  You want to know which machine to
  435. run your program on.  For comparing different machines (operating
  436. systems, etc.), the only way to compare that makes sense is to set up
  437. the machines in EXACTLY the way that they will NORMALLY be run, and then
  438. measure REAL time.  If the program will normally be run along with X,
  439. then run X.  If the program will normally be run on a dedicated
  440. workstation, then be sure nobody else is on the benchmarking machine.
  441. If the program will normally be run on a machine with three other Lisp
  442. jobs, then run three other Lisp jobs.  If the program will normally be
  443. run on a machine with 8meg of memory, then run with 8meg.  Here,
  444. "normal" means "normal for that machine".  If you the choice of an
  445. unloaded RT or a heavily loaded PMAX, do your benchmarking on an
  446. unloaded RT and a heavily loaded PMAX.
  447.  
  448. If you have a program you believe to be CPU intensive, then you might be
  449. tempted to compare "run" times across systems, hoping to get a meaningful
  450. result even if the benchmarking isn't done under the expected running
  451. condition.  Don't to this, for two reasons:
  452.      
  453.    * The operating systems might not compute run time in the same way.
  454.      
  455.    * Under the real running condition, the program might not be CPU
  456.      intensive after all.
  457.  
  458.  
  459.  
  460. In the end, only real time means anything -- it is the amount of time you
  461. have to wait for the result.  The only valid uses for run time are:
  462.      
  463.    * To develop insight into the program.  For example, if run time is
  464.      much less than elapsed time, then you are probably spending lots of
  465.      time paging.
  466.      
  467.    * To evaluate the relative performance of CPU intensive programs in
  468.      the same environment.
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476. 
  477. File: cmu-user.info  Node: UNIX Interface, Prev: Advanced Compiler Use and Efficiency Hints, Up: Top, Next: Event Dispatching with SERVE-EVENT
  478.  
  479. UNIX Interface
  480. **************
  481.  
  482.                    By Robert MacLachlan, Skef Wholey,
  483.  
  484.                      Bill Chiles, and William Lott
  485.  
  486.  
  487. CMU Common Lisp attempts to make the full power of the underlying
  488. environment available to the Lisp programmer.  This is done using
  489. combination of hand-coded interfaces and foreign function calls to C
  490. libraries.  Although the techniques differ, the style of interface is
  491. similar.  This chapter provides an overview of the facilities available
  492. and general rules for using them, as well as describing specific
  493. features in detail.  It is assumed that the reader has a working
  494. familiarity with Mach, Unix and X, as well as access to the standard
  495. system documentation.
  496.  
  497. * Menu:
  498.  
  499. * Reading the Command Line::    
  500. * Lisp Equivalents for C Routines::  
  501. * Type Translations::           
  502. * System Area Pointers::        
  503. * Unix System Calls::           
  504. * File Descriptor Streams::     
  505. * Making Sense of Mach Return Codes::  
  506. * Unix Interrupts::             
  507.  
  508.  
  509.  
  510. 
  511. File: cmu-user.info  Node: Reading the Command Line, Prev: UNIX Interface, Up: UNIX Interface, Next: Useful Variables
  512.  
  513. Reading the Command Line
  514. ========================
  515.  
  516.  
  517. The shell parses the command line with which Lisp is invoked, and passes
  518. a data structure containing the parsed information to Lisp.  This
  519. information is then extracted from that data structure and put into a
  520. set of Lisp data structures.
  521.  
  522.  
  523.  
  524.  -- Variable: *command-line-strings*
  525.  
  526.  -- Variable: *command-line-utility-name*
  527.  
  528.  -- Variable: *command-line-words*
  529.  
  530.  -- Variable: *command-line-switches*
  531.      The value of `*command-line-words*' is a list of strings that make
  532.      up the command line, one word per string.  The first word on the
  533.      command line, i.e.  the name of the program invoked (usually
  534.      `lisp') is stored in `*command-line-utility-name*'.  The value of
  535.      `*command-line-switches*' is a list of `command-line-switch'
  536.      structures, with a structure for each word on the command line
  537.      starting with a hyphen.  All the command line words between the
  538.      program name and the first switch are stored in
  539.      `*command-line-words*'.
  540.  
  541.  
  542. The following functions may be used to examine `command-line-switch'
  543. structures.
  544.  
  545.  -- Function: cmd-switch-name SWITCH
  546.  
  547.      Returns the name of the switch, less the preceding hyphen and
  548.      trailing equal sign (if any).
  549.  
  550.  
  551.  -- Function: cmd-switch-value SWITCH
  552.  
  553.      Returns the value designated using an embedded equal sign, if any.
  554.      If the switch has no equal sign, then this is null.
  555.  
  556.  
  557.  -- Function: cmd-switch-words SWITCH
  558.  
  559.      Returns a list of the words between this switch and the next switch
  560.      or the end of the command line.
  561.  
  562.  
  563.  
  564. 
  565. File: cmu-user.info  Node: Useful Variables, Prev: Reading the Command Line, Up: UNIX Interface, Next: Lisp Equivalents for C Routines
  566.  
  567.  
  568. Useful Variables
  569. ================
  570.  
  571.  
  572.  
  573.  
  574.  -- Variable: *stdin*
  575.  
  576.  -- Variable: *stdout*
  577.  
  578.  -- Variable: *stderr*
  579.      Streams connected to the standard input, output and error file
  580.      descriptors.
  581.  
  582.  
  583.  
  584.  
  585.  -- Variable: *tty*
  586.      A stream connected to `/dev/tty'.
  587.  
  588.  
  589.  
  590.  
  591.  -- Variable: *task-self*
  592.  
  593.  -- Variable: *task-data*
  594.  
  595.  -- Variable: *task-notify*
  596.      The initial ports for the Lisp process (Mach only.)
  597.  
  598.  
  599.  
  600. 
  601. File: cmu-user.info  Node: Lisp Equivalents for C Routines, Prev: Useful Variables, Up: UNIX Interface, Next: Type Translations
  602.  
  603. Lisp Equivalents for C Routines
  604. ===============================
  605.  
  606.  
  607. The UNIX documentation describes the system interface in terms of C
  608. procedure headers.  The corresponding Lisp function will have a somewhat
  609. different interface, since Lisp argument passing conventions and
  610. datatypes are different.
  611.  
  612. The main difference in the argument passing conventions is that Lisp
  613. does not support passing values by reference.  In Lisp, all argument and
  614. results are passed by value.  Interface functions take some fixed number
  615. of arguments and return some fixed number of values.  A given
  616. "parameter" in the C specification will appear as an argument, return
  617. value, or both, depending on whether it is an In parameter, Out
  618. parameter, or In/Out parameter.  The basic transformation one makes to
  619. come up with the Lisp equivalent of a C routine is to remove the Out
  620. parameters from the call, and treat them as extra return values.  In/Out
  621. parameters appear both as arguments and return values.  Since Out and
  622. In/Out parameters are only conventions in C, you must determine the
  623. usage from the documentation.
  624.  
  625.  
  626. Thus, the C routine declared as
  627.      
  628.      kern_return_t lookup(servport, portsname, portsid)
  629.              port        servport;
  630.              char        *portsname;
  631.              int        *portsid;        /* out */
  632.       {
  633.        ...
  634.        *portsid = <expression to compute portsid field>
  635.        return(KERN_SUCCESS);
  636.       }
  637.  
  638. has as its Lisp equivalent something like
  639.      
  640.      (defun lookup (ServPort PortsName)
  641.        ...
  642.        (values
  643.         success
  644.         <expression to compute portsid field>))
  645.  
  646. If there are multiple out or in-out arguments, then there are multiple
  647. additional returns values.
  648.  
  649. Fortunately, CMU Common Lisp programmers rarely have to worry about the
  650. nuances of this translation process, since the names of the arguments
  651. and return values are documented in a way so that the `describe'
  652. function (and the Hemlock `Describe Function Call' command, invoked with
  653. C-M-Shift-A) will list this information.  Since the names of arguments
  654. and return values are usually descriptive, the information that
  655. `describe' prints is usually all one needs to write a call. Most
  656. programmers use this on-line documentation nearly all of the time, and
  657. thereby avoid the need to handle bulky manuals and perform the
  658. translation from barbarous tongues.
  659.  
  660. 
  661. File: cmu-user.info  Node: Type Translations, Prev: Lisp Equivalents for C Routines, Up: UNIX Interface, Next: System Area Pointers
  662.  
  663. Type Translations
  664. =================
  665.  
  666.  
  667. Lisp data types have very different representations from those used by
  668. conventional languages such as C.  Since the system interfaces are
  669. designed for conventional languages, Lisp must translate objects to and
  670. from the Lisp representations.  Many simple objects have a direct
  671. translation: integers, characters, strings and floating point numbers
  672. are translated to the corresponding Lisp object.  A number of types,
  673. however, are implemented differently in Lisp for reasons of clarity and
  674. efficiency.
  675.  
  676. Instances of enumerated types are expressed as keywords in Lisp.
  677. Records, arrays, and pointer types are implemented with the Alien
  678. facility (see page ?.)  Access functions are defined for these types
  679. which convert fields of records, elements of arrays, or data referenced
  680. by pointers into Lisp objects (possibly another object to be referenced
  681. with another access function).
  682.  
  683. One should dispose of Alien objects created by constructor functions or
  684. returned from remote procedure calls when they are no longer of any use,
  685. freeing the virtual memory associated with that object.  Since aliens
  686. contain pointers to non-Lisp data, the garbage collector cannot do this
  687. itself.  If the memory was obtained from make-alien ? or from a foreign
  688. function call to a routine that used `malloc', then free-alien ? should
  689. be used.  If the alien was created using MACH memory allocation (e.g.
  690. `vm_allocate'), then the storage should be freed using `vm_deallocate'.
  691.  
  692. 
  693. File: cmu-user.info  Node: System Area Pointers, Prev: Type Translations, Up: UNIX Interface, Next: Unix System Calls
  694.  
  695. System Area Pointers
  696. ====================
  697.  
  698.  
  699. Note that in some cases an address is represented by a Lisp integer, and
  700. in other cases it is represented by a real pointer.  Pointers are
  701. usually used when an object in the current address space is being
  702. referred to.  The MACH virtual memory manipulation calls must use
  703. integers, since in principle the address could be in any process, and
  704. Lisp cannot abide random pointers.  Because these types are represented
  705. differently in Lisp, one must explicitly coerce between these
  706. representations.
  707.  
  708. System Area Pointers (SAPs) provide a mechanism that bypasses the Alien
  709. type system and accesses virtual memory directly.  A SAP is a raw byte
  710. pointer into the `lisp' process address space.  SAPs are represented
  711. with a pointer descriptor, so SAP creation can cause consing.  However,
  712. the compiler uses a non-descriptor representation for SAPs when
  713. possible, so the consing overhead is generally minimal.  *Note
  714. Non-Descriptor Representations::.
  715.  
  716.  
  717.  -- Function: sap-int SAP
  718.  
  719.  -- Function: int-sap INT
  720.  
  721.      The function `sap-int' is used to generate an integer corresponding
  722.      to the system area pointer, suitable for passing to the kernel
  723.      interfaces (which want all addresses specified as integers).  The
  724.      function `int-sap' is used to do the opposite conversion.  The
  725.      integer representation of a SAP is the byte offset of the SAP from
  726.      the start of the address space.
  727.  
  728.  
  729.  
  730.  -- Function: sap+ SAP OFFSET
  731.  
  732.      This function adds a byte OFFSET to SAP, returning a new SAP.
  733.  
  734.  
  735.  
  736.  -- Function: sap-ref-8 SAP OFFSET
  737.  
  738.  -- Function: sap-ref-16 SAP OFFSET
  739.  
  740.  -- Function: sap-ref-32 SAP OFFSET
  741.  
  742.      These functions return the 8, 16 or 32 bit unsigned integer at
  743.      OFFSET from SAP.  The OFFSET is always a byte offset, regardless of
  744.      the number of bits accessed.  `setf' may be used with the these
  745.      functions to deposit values into virtual memory.
  746.  
  747.  
  748.  
  749.  -- Function: signed-sap-ref-8 SAP OFFSET
  750.  
  751.  -- Function: signed-sap-ref-16 SAP OFFSET
  752.  
  753.  -- Function: signed-sap-ref-32 SAP OFFSET
  754.  
  755.      These functions are the same as the above unsigned operations,
  756.      except that they sign-extend, returning a negative number if the
  757.      high bit is set.
  758.  
  759.  
  760. 
  761. File: cmu-user.info  Node: Unix System Calls, Prev: System Area Pointers, Up: UNIX Interface, Next: File Descriptor Streams
  762.  
  763. Unix System Calls
  764. =================
  765.  
  766.  
  767. You probably won't have much cause to use them, but all the Unix system
  768. calls are available.  The Unix system call functions are in the `Unix'
  769. package.  The name of the interface for a particular system call is the
  770. name of the system call prepended with `unix-'.  The system usually
  771. defines the associated constants without any prefix name.  To find out
  772. how to use a particular system call, try using `describe' on it.  If
  773. that is unhelpful, look at the source in `syscall.lisp' or consult your
  774. system maintainer.
  775.  
  776. The Unix system calls indicate an error by returning false as the first
  777. value and the Unix error number as the second value.  If the call
  778. succeeds, then the first value will always be non-nil, often `t'.
  779.  
  780.  
  781.  -- Function: get-unix-error-msg ERROR
  782.  
  783.      This function returns a string describing the Unix error number
  784.      ERROR.
  785.  
  786.  
  787. 
  788. File: cmu-user.info  Node: File Descriptor Streams, Prev: Unix System Calls, Up: UNIX Interface, Next: Making Sense of Mach Return Codes
  789.  
  790. File Descriptor Streams
  791. =======================
  792.  
  793.  
  794. Many of the UNIX system calls return file descriptors.  Instead of using
  795. other UNIX system calls to perform I/O on them, you can create a stream
  796. around them.  For this purpose, fd-streams exist.
  797.  
  798.  
  799.  
  800.  -- Function: make-fd-stream DESCRIPTOR
  801.          &keys :input :output :element-type
  802.               :buffering :name :file :original
  803.               :delete-original :auto-close
  804.               :timeout
  805.  
  806.      This function creates a file descriptor stream using DESCRIPTOR.
  807.      If INPUT is non-nil, input operations are allowed.  If
  808.      OUTPUT is non-nil, output operations are allowed.  The default is
  809.      input only.  These keywords are defined:
  810.      ELEMENT-TYPE     
  811.            is the type of the unit of transaction for the stream, which
  812.           defaults to `string-char'.  See the Common Lisp description of
  813.           `open' for valid values.
  814.           
  815.      BUFFERING     
  816.            is the kind of output buffering desired for the stream.
  817.           Legal values are :none for no buffering, :line for buffering
  818.           up to each newline, and :full for full buffering.
  819.           
  820.      NAME     
  821.            is a simple-string name to use for descriptive purposes when
  822.           the system prints an fd-stream.  When printing fd-streams, the
  823.           system prepends the streams name with `Stream for '.  If NAME
  824.           is unspecified, it defaults to a string containing FILE or
  825.           DESCRIPTOR, in order of preference.
  826.           
  827.      FILE, ORIGINAL     
  828.           : FILE specifies the name of the associated file when creating
  829.           a file stream (must be a `simple-string'). ORIGINAL is the
  830.           `simple-string' name of a backup file containing the original
  831.           contents of FILE while writing FILE.
  832.           
  833.           When you abort the stream by passing true to `close' as the
  834.           second argument, if you supplied both FILE and ORIGINAL,
  835.           `close' will rename the ORIGINAL name to the FILE name.  When
  836.           you `close' the stream normally, if you supplied ORIGINAL, and
  837.           DELETE-ORIGINAL is non-nil, `close' deletes ORIGINAL.  If
  838.           AUTO-CLOSE is true (the default), then DESCRIPTOR will be
  839.           closed when the stream is garbage collected.
  840.           
  841.      TIMEOUT     
  842.            if non-null, then TIMEOUT is an integer number of seconds
  843.           after which an input wait should time out.  If a read does
  844.           time out, then the `system:io-timeout' condition is signalled.
  845.      
  846.  
  847.  
  848.  
  849.  -- Function: fd-stream-p OBJECT
  850.  
  851.      This function returns true if OBJECT is an fd-stream, and nil if
  852.      not.
  853.  
  854.  
  855.  
  856.  -- Function: fd-stream-fd STREAM
  857.  
  858.      This returns the file descriptor associated with STREAM.
  859.  
  860.  
  861.  
  862. 
  863. File: cmu-user.info  Node: Making Sense of Mach Return Codes, Prev: File Descriptor Streams, Up: UNIX Interface, Next: Unix Interrupts
  864.  
  865. Making Sense of Mach Return Codes
  866. =================================
  867.  
  868.  
  869. Whenever a remote procedure call returns a Unix error code (such as
  870. `kern_return_t'), it is usually prudent to check that code to see if the
  871. call was successful.  To relieve the programmer of the hassle of testing
  872. this value himself, and to centralize the information about the meaning
  873. of non-success return codes, CMU Common Lisp provides a number of macros
  874. and functions.  See also get-unix-error-msg *Note Unix System Calls::.
  875.  
  876.  
  877.  -- Function: gr-error 
  878.         FUNCTION GR &optional CONTEXT
  879.  
  880.      Signals a Lisp error, printing a message indicating that the call to the
  881.      specified FUNCTION failed, with the return code GR.  If supplied, the
  882.      CONTEXT string is printed after the FUNCTION name and before the string
  883.      associated with the GR.  For example:
  884.           
  885.           * (gr-error 'nukegarbage 3 "lost big")
  886.           
  887.           Error in function GR-ERROR:
  888.           NUKEGARBAGE lost big, no space.
  889.           Proceed cases:
  890.           0: Return to Top-Level.
  891.           Debug  (type H for help)
  892.           (Signal #<Conditions:Simple-Error.5FDE0>)
  893.           0] 
  894.      
  895.  
  896.  
  897.  
  898.  -- Macro: gr-call FUNCTION &rest ARGS
  899.  
  900.  -- Macro: gr-call* FUNCTION &rest ARGS
  901.  
  902.      These macros can be used to call a function and automatically check the
  903.      GeneralReturn code and signal an appropriate error in case of non-successful
  904.      return.  `gr-call' returns false if no error occurs, while `gr-call*'
  905.      returns the second value of the function called.
  906.           
  907.           * (gr-call mach:port_allocate *task-self*)
  908.           NIL
  909.           * 
  910.      
  911.  
  912.  
  913.  
  914.  -- Macro: gr-bind 
  915.         `('{VAR}*`)' `('FUNCTION {ARG}*`)' {FORM}*
  916.  
  917.      This macro can be used much like `multiple-value-bind' to bind the VARs
  918.      to return values resulting from calling the FUNCTION with the given
  919.      ARGs.  The first return value is not bound to a variable, but is checked as a
  920.      GeneralReturn code, as in `gr-call'.
  921.           
  922.           * (gr-bind (port_list port_list_cnt)
  923.                      (mach:port_select *task-self*)
  924.               (format t "The port count is ~S." port_list_cnt)
  925.               port_list)
  926.           The port count is 0.
  927.           #<Alien value>
  928.           * 
  929.      
  930.  
  931.  
  932. 
  933. File: cmu-user.info  Node: Unix Interrupts, Prev: Making Sense of Mach Return Codes, Up: UNIX Interface
  934.  
  935. Unix Interrupts
  936. ===============
  937.  
  938.  
  939.  
  940. CMU Common Lisp allows access to all the Unix signals that can be
  941. generated under Unix.  It should be noted that if this capability is
  942. abused, it is possible to completely destroy the running Lisp.  The
  943. following macros and functions allow access to the Unix interrupt
  944. system.  The signal names as specified in section 2 of the Unix
  945. Programmer's Manual are exported from the Unix package.
  946.  
  947. * Menu:
  948.  
  949. * Changing Interrupt Handlers::  
  950. * Examples of Signal Handlers::  
  951.  
  952.  
  953. 
  954. File: cmu-user.info  Node: Changing Interrupt Handlers, Prev: Unix Interrupts, Up: Unix Interrupts, Next: Examples of Signal Handlers
  955.  
  956. Changing Interrupt Handlers
  957. ---------------------------
  958.  
  959.  
  960.  
  961.  
  962.  -- Macro: with-enabled-interrupts 
  963.         SPECS &rest BODY
  964.  
  965.      This macro should be called with a list of signal specifications,
  966.      SPECS.  Each element of SPECS should be a list of two elements: the
  967.      first should be the Unix signal for which a handler should be
  968.      established, the second should be a function to be called when the
  969.      signal is received One or more signal handlers can be established
  970.      in this way.  `with-enabled-interrupts' establishes the correct
  971.      signal handlers and then executes the forms in BODY.  The forms are
  972.      executed in an unwind-protect so that the state of the signal
  973.      handlers will be restored to what it was before the
  974.      `with-enabled-interrupts' was entered.  A signal handler function
  975.      specified as NIL will set the Unix signal handler to the default
  976.      which is normally either to ignore the signal or to cause a core
  977.      dump depending on the particular signal.
  978.  
  979.  
  980.  
  981.  -- Macro: without-interrupts &rest BODY
  982.  
  983.      It is sometimes necessary to execute a piece a code that can not be
  984.      interrupted.  This macro the forms in BODY with interrupts
  985.      disabled.  Note that the Unix interrupts are not actually disabled,
  986.      rather they are queued until after BODY has finished executing.
  987.  
  988.  
  989.  
  990.  -- Macro: with-interrupts &rest BODY
  991.  
  992.      When executing an interrupt handler, the system disables
  993.      interrupts, as if the handler was wrapped in in a
  994.      `without-interrupts'.  The macro `with-interrupts' can be used to
  995.      enable interrupts while the forms in BODY are evaluated.  This is
  996.      useful if BODY is going to enter a break loop or do some long
  997.      computation that might need to be interrupted.
  998.  
  999.  
  1000.  
  1001.  -- Macro: without-hemlock &rest BODY
  1002.  
  1003.      For some interrupts, such as SIGTSTP (suspend the Lisp process and
  1004.      return to the Unix shell) it is necessary to leave Hemlock and then
  1005.      return to it.  This macro executes the forms in BODY after exiting
  1006.      Hemlock.  When BODY has been executed, control is returned to
  1007.      Hemlock.
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  -- Function: enable-interrupt 
  1013.        SIGNAL FUNCTION
  1014.  
  1015.      This function establishes FUNCTION as the handler for SIGNAL.
  1016.        Unless you want to establish a global signal
  1017.      handler, you should use the macro `with-enabled-interrupts' to temporarily
  1018.      establish a signal handler.  
  1019.      `enable-interrupt' returns the old function associated with the signal.
  1020.      
  1021.  
  1022.  
  1023.  
  1024.  -- Function: ignore-interrupt SIGNAL
  1025.  
  1026.      Ignore-interrupt sets the Unix signal mechanism to ignore SIGNAL
  1027.      which means that the Lisp process will never see the signal.
  1028.      Ignore-interrupt returns the old function associated with the
  1029.      signal or false if none is currently defined.
  1030.  
  1031.  
  1032.  
  1033.  -- Function: default-interrupt SIGNAL
  1034.  
  1035.      Default-interrupt can be used to tell the Unix signal mechanism to
  1036.      perform the default action for SIGNAL.  For details on what the
  1037.      default action for a signal is, see section 2 of the Unix
  1038.      Programmer's Manual.  In general, it is likely to ignore the signal
  1039.      or to cause a core dump.
  1040.  
  1041.  
  1042. 
  1043. File: cmu-user.info  Node: Examples of Signal Handlers, Prev: Changing Interrupt Handlers, Up: Unix Interrupts
  1044.  
  1045. Examples of Signal Handlers
  1046. ---------------------------
  1047.  
  1048.  
  1049. The following code is the signal handler used by the Lisp system for the
  1050. SIGINT signal.
  1051.      
  1052.      (defun ih-sigint (signal code scp)
  1053.        (declare (ignore signal code scp))
  1054.        (without-hemlock
  1055.         (with-interrupts
  1056.          (break "Software Interrupt" t))))
  1057.  
  1058. The `without-hemlock' form is used to make sure that Hemlock is exited
  1059. before a break loop is entered.  The `with-interrupts' form is used to
  1060. enable interrupts because the user may want to generate an interrupt
  1061. while in the break loop.  Finally, break is called to enter a break
  1062. loop, so the user can look at the current state of the computation.  If
  1063. the user proceeds from the break loop, the computation will be restarted
  1064. from where it was interrupted.
  1065.  
  1066. The following function is the Lisp signal handler for the SIGTSTP signal
  1067. which suspends a process and returns to the Unix shell.
  1068.      
  1069.      (defun ih-sigtstp (signal code scp)
  1070.        (declare (ignore signal code scp))
  1071.        (without-hemlock
  1072.         (Unix:unix-kill (Unix:unix-getpid) Unix:sigstop)))
  1073.  
  1074. Lisp uses this interrupt handler to catch the SIGTSTP signal because it
  1075. is necessary to get out of Hemlock in a clean way before returning to
  1076. the shell.
  1077.  
  1078. To set up these interrupt handlers, the following is recommended:
  1079.      
  1080.      (with-enabled-interrupts ((Unix:SIGINT #'ih-sigint)
  1081.                                (Unix:SIGTSTP #'ih-sigtstp))
  1082.        <user code to execute with the above signal handlers enabled.>
  1083.      )
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089. 
  1090. File: cmu-user.info  Node: Event Dispatching with SERVE-EVENT, Prev: UNIX Interface, Up: Top, Next: Alien Objects
  1091.  
  1092. Event Dispatching with SERVE-EVENT
  1093. **********************************
  1094.  
  1095.                   By Bill Chiles and Robert MacLachlan
  1096.  
  1097.  
  1098. It is common to have multiple activities simultaneously operating in the
  1099. same Lisp process.  Furthermore, Lisp programmers tend to expect a
  1100. flexible development environment.  It must be possible to load and
  1101. modify application programs without requiring modifications to other
  1102. running programs.  CMU Common Lisp achieves this by having a central
  1103. scheduling mechanism based on an event-driven, object-oriented paradigm.
  1104.  
  1105. An EVENT is some interesting happening that should cause the Lisp
  1106. process to wake up and do something.  These events include X events and
  1107. activity on Unix file descriptors.  The object-oriented mechanism is
  1108. only available with the first two, and it is optional with X events as
  1109. described later in this chapter.  In an X event, the window ID is the
  1110. object capability and the X event type is the operation code.  The Unix
  1111. file descriptor input mechanism simply consists of an association list
  1112. of a handler to call when input shows up on a particular file
  1113. descriptor.
  1114.  
  1115.  
  1116. * Menu:
  1117.  
  1118. * Object Sets::                 
  1119. * The SERVE-EVENT Function::    
  1120. * Using SERVE-EVENT with Unix File Descriptors::  
  1121. * Using SERVE-EVENT with the CLX Interface to X::  
  1122. * A SERVE-EVENT Example::       
  1123.  
  1124.  
  1125. 
  1126. File: cmu-user.info  Node: Object Sets, Prev: Event Dispatching with SERVE-EVENT, Up: Event Dispatching with SERVE-EVENT, Next: The SERVE-EVENT Function
  1127.  
  1128. Object Sets
  1129. ===========
  1130.  
  1131. An object set is a collection of objects that have the same implementation
  1132. for each operation.  Externally the object is represented by the object
  1133. capability and the operation is represented by the operation code.  Within
  1134. Lisp, the object is represented by an arbitrary Lisp object, and the
  1135. implementation for the operation is represented by an arbitrary Lisp function.
  1136. The object set mechanism maintains this translation from the external to the
  1137. internal representation.
  1138.  
  1139.  
  1140.   -- Function: make-object-set 
  1141.         NAME &optional DEFAULT-HANDLER
  1142.  
  1143.      This function makes a new object set.  NAME is a string used
  1144.      only for purposes of identifying the object set when it is printed.
  1145.      DEFAULT-HANDLER is the function used as a handler when an
  1146.      undefined operation occurs on an object in the set.  You can define
  1147.      operations with the `serve-'OPERATION functions exported
  1148.      the `extensions' package for X events
  1149.      (?).  Objects are added with
  1150.      `system:add-xwindow-object'.  Initially the object set has no
  1151.      objects and no defined operations.
  1152.  
  1153.  
  1154.  
  1155.  
  1156.   -- Function: object-set-operation 
  1157.         OBJECT-SET OPERATION-CODE
  1158.  
  1159.      This function returns the handler function that is the
  1160.      implementation of the operation corresponding to OPERATION-CODE
  1161.      in OBJECT-SET.  When set with `setf', the setter function
  1162.      establishes the new handler.  The `serve-'OPERATION
  1163.      functions exported from the `extensions' package for X events
  1164.      (?) call this on behalf of the user when
  1165.      announcing a new operation for an object set.
  1166.  
  1167.  
  1168.  
  1169.  -- Function: add-xwindow-object 
  1170.         WINDOW OBJECT OBJECT-SET
  1171.  
  1172.      These functions add PORT or WINDOW to OBJECT-SET.  OBJECT is
  1173.      an arbitrary Lisp object that is associated with the PORT or WINDOW
  1174.      capability.  WINDOW is a CLX window.  When an event occurs,
  1175.      `system:serve-event' passes OBJECT as an argument to the handler
  1176.      function.
  1177.  
  1178.  
  1179.  
  1180. 
  1181. File: cmu-user.info  Node: The SERVE-EVENT Function, Prev: Object Sets, Up: Event Dispatching with SERVE-EVENT, Next: Using SERVE-EVENT with Unix File Descriptors
  1182.  
  1183. The SERVE-EVENT Function
  1184. ========================
  1185.  
  1186.  
  1187. The `system:serve-event' function is the standard way for an application
  1188. to wait for something to happen.  For example, the Lisp system calls
  1189. `system:serve-event' when it wants input from X or a terminal stream.
  1190. The idea behind `system:serve-event' is that it knows the appropriate
  1191. action to take when any interesting event happens.  If an application calls
  1192. `system:serve-event' when it is idle, then any other applications with
  1193. pending events can run.  This allows several applications to run "at the
  1194. same time" without interference, even though there is only one thread of
  1195. control.  Note that if an application is waiting for input of any kind,
  1196. then other applications will get events.
  1197.  
  1198.  
  1199.  -- Function: serve-event &optional TIMEOUT
  1200.  
  1201.      This function waits for an event to happen and then dispatches to the
  1202.      correct handler function.  If specified, TIMEOUT is the number of
  1203.      seconds to wait before timing out.  A time out of zero seconds is legal and
  1204.      causes `system:serve-event' to poll for any events immediately available
  1205.      for processing.  `system:serve-event' returns true if it serviced at
  1206.      least one event, and nil otherwise.  Depending on the application, when
  1207.      `system:serve-event' returns true, you might want to call it repeatedly
  1208.      with a timeout of zero until it returns nil.
  1209.      
  1210.      If input is available on any designated file descriptor, then this calls the
  1211.      appropriate handler function supplied by `system:add-fd-handler'.
  1212.      
  1213.      Since events for many different applications may arrive
  1214.      simultaneously, an application waiting for a specific event
  1215.      must loop on `system:serve-event' until the desired event
  1216.      happens.  Since programs such as hemlock call
  1217.      `system:serve-event' for input, applications usually do
  1218.      not need to call `system:serve-event' at all; hemlock
  1219.      allows other application's handlers to run when it goes into an
  1220.      input wait.
  1221.  
  1222.  
  1223.  
  1224.  -- Function: serve-all-events &optional TIMEOUT
  1225.  
  1226.      This function is similar to `system:serve-event', except it serves all
  1227.      the pending events rather than just one.  It returns true if it serviced
  1228.      at least one event, and nil otherwise.
  1229.  
  1230.  
  1231.  
  1232. 
  1233. File: cmu-user.info  Node: Using SERVE-EVENT with Unix File Descriptors, Prev: The SERVE-EVENT Function, Up: Event Dispatching with SERVE-EVENT, Next: Using SERVE-EVENT with the CLX Interface to X
  1234.  
  1235. Using SERVE-EVENT with Unix File Descriptors
  1236. ============================================
  1237.  
  1238. Object sets are not available for use with file descriptors, as there are
  1239. only two operations possible on file descriptors: input and output.
  1240. Instead, a handler for either input or output can be registered with
  1241. `system:serve-event' for a specific file descriptor.  Whenever any input
  1242. shows up, or output is possible on this file descriptor, the function
  1243. associated with the handler for that descriptor is funcalled with the
  1244. descriptor as it's single argument.
  1245.  
  1246.  
  1247.  -- Function: add-fd-handler fd direction function
  1248.  
  1249.      This function installs and returns a new handler for the file descriptor
  1250.      FD.  DIRECTION can be either :input if the system should invoke
  1251.      the handler when input is available or :output if the system should invoke
  1252.      the handler when output is possible.  This returns a unique object representing
  1253.      the handler, and this is a suitable argument for `system:remove-fd-handler'
  1254.      FUNCTION must take one argument, the file descriptor.
  1255.  
  1256.  
  1257.  
  1258.  -- Function: remove-fd-handler HANDLER
  1259.  
  1260.      This function removes HANDLER, that `add-fd-handler' must have previously
  1261.      returned.
  1262.  
  1263.  
  1264.  
  1265.   -- Macro: with-fd-handler 
  1266.         (direction fd function) {FORM}*
  1267.  
  1268.      This macro executes the supplied forms with a handler installed using FD,
  1269.      DIRECTION, and FUNCTION.  See `system:add-fd-handler'.
  1270.  
  1271.  
  1272.  
  1273.   -- Function: wait-until-fd-usable 
  1274.         direction fd &optional TIMEOUT
  1275.  
  1276.      This function waits for up to TIMEOUT seconds for FD to become usable
  1277.      for DIRECTION (either :input or :output).  If TIMEOUT is nil
  1278.      or unspecified, this waits forever.
  1279.  
  1280.  
  1281.  
  1282.  -- Function: invalidate-descriptor FD
  1283.  
  1284.      This function removes all handlers associated with FD.  This should only be
  1285.      used in drastic cases (such as I/O errors, but not necessarily EOF).  Normally,
  1286.      you should use `remove-fd-handler' to remove the specific handler.
  1287.  
  1288.  
  1289.  
  1290. 
  1291. File: cmu-user.info  Node: Using SERVE-EVENT with the CLX Interface to X, Prev: Using SERVE-EVENT with Unix File Descriptors, Up: Event Dispatching with SERVE-EVENT, Next: A SERVE-EVENT Example
  1292.  
  1293. Using SERVE-EVENT with the CLX Interface to X
  1294. =============================================
  1295.  
  1296. Remember from section *Note Object Sets::, an object set is a collection of
  1297. objects, CLX windows in this case, with some set of operations, event keywords,
  1298. with corresponding implementations, the same handler functions.  Since X allows
  1299. multiple display connections from a given process, you can avoid using object
  1300. sets if every window in an application or display connection behaves the same.
  1301. If a particular X application on a single display connection has windows that
  1302. want to handle certain events differently, then using object sets is a
  1303. convenient way to organize this since you need some way to map the window/event
  1304. combination to the appropriate functionality.
  1305.  
  1306. The following is a discussion of functions exported from the `extensions'
  1307. package that facilitate handling CLX events through `system:serve-event'.
  1308. The first two routines are useful regardless of whether you use
  1309. `system:serve-event':
  1310.  
  1311.   -- Function: open-clx-display 
  1312.        &optional STRING
  1313.  
  1314.      This function parses STRING for an X display specification
  1315.      including display and screen numbers.  STRING defaults to the
  1316.      following: (cdr (assoc :display ext:*environment-list* :test #'eq))
  1317.      If any field in the display specification is missing, this signals
  1318.      an error.  `ext:open-clx-display' returns the CLX display and
  1319.      screen.
  1320.  
  1321.  
  1322.  
  1323.  -- Function: flush-display-events DISPLAY
  1324.  
  1325.      This function flushes all the events in DISPLAY's event queue
  1326.      including the current event, in case the user calls this from
  1327.      within an event handler.
  1328.  
  1329.  
  1330.  
  1331. * Menu:
  1332.  
  1333. * Without Object Sets::         
  1334. * With Object Sets::            
  1335.  
  1336.  
  1337. 
  1338. File: cmu-user.info  Node: Without Object Sets, Prev: Using SERVE-EVENT with the CLX Interface to X, Up: Using SERVE-EVENT with the CLX Interface to X, Next: With Object Sets
  1339.  
  1340. Without Object Sets
  1341. -------------------
  1342.  
  1343. Since most applications that use CLX, can avoid the complexity of object
  1344. sets, these routines are described in a separate section.  The routines
  1345. described in the next section that use the object set mechanism are
  1346. based on these interfaces.
  1347.  
  1348.  
  1349.   -- Function: enable-clx-event-handling 
  1350.        DISPLAY HANDLER
  1351.  
  1352.      This function causes `system:serve-event' to notice when there is input
  1353.      on DISPLAY's connection to the X11 server.  When this happens,
  1354.      `system:serve-event' invokes HANDLER on DISPLAY in a dynamic
  1355.      context with an error handler bound that flushes all events from
  1356.      DISPLAY and returns.  By returning, the error handler declines to
  1357.      handle the error, but it will have cleared all events; thus, entering the
  1358.      debugger will not result in infinite errors due to streams that wait via
  1359.      `system:serve-event' for input.  Calling this repeatedly on the same
  1360.      DISPLAY establishes HANDLER as a new handler, replacing any
  1361. previous one for DISPLAY.  
  1362.  
  1363.  
  1364.  -- Function: disable-clx-event-handling DISPLAY
  1365.  
  1366.      This function undoes the effect of `ext:enable-clx-event-handling'.
  1367.  
  1368.  
  1369.  
  1370.   -- Macro: with-clx-event-handling 
  1371.           (DISPLAY HANDLER) {form}*
  1372.  
  1373.      This macro evaluates each FORM in a context where
  1374.      `system:serve-event' invokes HANDLER on DISPLAY whenever there is
  1375.      input on DISPLAY's connection to the X server.  This destroys any
  1376.      previously established handler for DISPLAY.
  1377.  
  1378.  
  1379.  
  1380. 
  1381.